package com.zygh.questionnairesurvey.server;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zygh.questionnairesurvey.common.CommonResult;
import com.zygh.questionnairesurvey.entity.Dept;
import com.zygh.questionnairesurvey.mapper.DeptMapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

import static cn.hutool.core.date.DateTime.now;

@Service
public class DeptService extends ServiceImpl<DeptMapper, Dept> {

    @Resource
    private DeptMapper deptMapper;

    /**
     * 查询所有记录
     *
     * @return 返回集合，没有返回空List
     */
    public CommonResult listAll(Integer id, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<Dept> depts = deptMapper.selectList(Wrappers.<Dept>lambdaQuery()
                .eq(Dept::getPid, 0)
                .eq(id != null && id != 0, Dept::getDeptId, id)
                .orderByAsc(Dept::getSort));
        depts.forEach(dept -> {
            List<Dept> depts1 = deptMapper.selectList(Wrappers.<Dept>lambdaQuery().eq(Dept::getPid, dept.getDeptId()));
            if (depts1 != null) {
                dept.setDateList(depts1);
            }
        });
        if (depts.size() > 0) {
            PageInfo<Dept> pageInfo = new PageInfo<>(depts);
            return new CommonResult().success(pageInfo);
        }
        return new CommonResult().validateFailed("查询失败！");
    }


    /**
     * 根据主键查询
     *
     * @param deptId 主键
     * @return 返回记录，没有返回null
     */
    public CommonResult getById(Integer deptId) {
        Dept depts = deptMapper.selectById(deptId);
        if (depts != null) {
            return new CommonResult().success(depts);
        }
        return new CommonResult().validateFailed("查询失败！");
    }

    /**
     * 新增，插入所有字段
     *
     * @param dept 新增的记录
     * @return 返回影响行数
     */
    public CommonResult insert(Dept dept) {
        dept.setCreateTime(now());
        int insert = deptMapper.insert(dept);
        if (insert > 0) {
            return new CommonResult().success();
        }
        return new CommonResult().validateFailed("新增失败！");
    }


    /**
     * 修改，修改所有字段
     *
     * @param dept 修改的记录
     * @return 返回影响行数
     */
    public CommonResult update(Dept dept) {
        dept.setUpdateTime(now());
        int i = deptMapper.updateById(dept);
        if (i > 0) {
            return new CommonResult().success();
        }
        return new CommonResult().validateFailed("修改失败！");
    }





    /**
     * 删除记录
     *
     * @param id 待删除的记录
     * @return 返回影响行数
     */
    public CommonResult delete(Integer id) {
        int i = deptMapper.deleteById(id);
        if (i > 0) {
            deptMapper.delete(Wrappers.<Dept>lambdaQuery().eq(Dept::getPid, id));
            return new CommonResult().success();
        }
        return new CommonResult().validateFailed("删除失败！");
    }

    /**
     * 根據id查询下级部门
     *
     * @param id
     * @return
     */
    public CommonResult findDeptByid(Integer id, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<Dept> depts = deptMapper.selectList(Wrappers.<Dept>lambdaQuery()
                .eq(Dept::getPid, id)
                .orderByAsc(Dept::getDeptId));
        PageInfo<Dept> pageInfo = new PageInfo<>(depts);
        return new CommonResult().success(pageInfo);
    }

}